/*
 * @Author: your name
 * @Date: 2021-06-08 12:06:33
 * @LastEditTime: 2021-06-16 12:31:20
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \UDP\SERVER_\database.cpp
 */
#include "database.h"

Database::Database()
{
    //建立连接
    rc = sqlite3_open("data.db", &db);

   if( rc ){
      fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
      exit(0);
   }else{
      //fprintf(stderr, "Opened database successfully\n");
   }
}

Database::~Database()
{
    //释放连接
    sqlite3_close(db);
}

bool Database::query_SQL(char* sql)
{
    rc = sqlite3_exec(db, sql, NULL, NULL, &zErrMsg);
    if( rc != SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        return false;
    }else{
        fprintf(stdout, "Operation done successfully\n");
        return true;
    }
}

bool Database::show_Table(char* table)
{
    //构造查询语句
    char sql[SQL_MAX_LENGTH] = "select * from ";
    char* tem = ";";

    strcat(sql,table);
    strcat(sql,tem);
    // cout << "sql:";
    // puts(sql); 
    
    rc = sqlite3_get_table(db, sql, &pResult, &nRow, &nCol, NULL);
    if (rc == SQLITE_OK)
    {
        cout << "the datas can be used are as follows:\n";

        for(int k=0; k<=nRow; k++)
        {
            for (int i = 0; i < nCol; i++) 
            {
                cout << pResult[nCol*k + i] << "\t";
            }
            cout << endl;
        }

        
        cout << endl;
        return true;
    }
    else
    {
        cout << "数据库不存在该表" << endl;
        return false;
    }
}

bool Database::query_Table(char* sql)
{
    // printf("查询语句：%s\n",sql);
    rc = sqlite3_get_table(db, sql, &pResult, &nRow, &nCol, NULL);
    cout << "查询,nRow = " << nRow << endl;
    if(nRow > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int Database::verify_User_Passwd(char* sql_str,info& s_info)
{
    rc = sqlite3_get_table(db, sql_str, &pResult, &nRow, &nCol, NULL);
    
    if(nRow > 0)
    {
        // cout << "Find User In Database" << endl;
        //将用户类型，余额，购物车数目，购物车信息（商品+商品数）写入s_info
        DealStr ds;
        s_info.type = ds.str_To_Num(pResult[nCol+3]);
        s_info.balance = ds.str_To_Num(pResult[nCol+4]);
        s_info.cartnum = ds.str_To_Num(pResult[nCol+5]);
        
        ds.str_To_Array(pResult[nCol+6],s_info.cartnum,s_info.cartIndexArray);
        ds.str_To_Array(pResult[nCol+7],s_info.cartnum,s_info.indexNumArray);

       {
            // cout << "type:" << s_info.type 
            // << " balance:" << s_info.balance
            // << " cartnum:" << s_info.cartnum << endl;
            // cout << "购物车商品：";
            // for(int i=0; i<s_info.cartnum; i++)
            // {
            //     cout << s_info.cartIndexArray[i] << "  ";
            // }
            // cout << endl;
            // cout << "购物车数目：";
            // for(int i=0; i<s_info.cartnum; i++)
            // {
            //     cout << s_info.indexNumArray[i] << "  ";
            // }
            // cout << endl;
       }
        return pResult[nCol+3][0] == '1' ? 1 : 2;
    }
    //未查询到该用户
    else
    {
        cout << "not find the user" << endl;
        s_info.type = _LOGIN_ERROR;
        return 0;
    }
}

int Database::get_All_Commodity_Info(info& s_info)
{
    char* sql = "select * from goods;";
    rc = sqlite3_get_table(db, sql, &pResult, &nRow, &nCol, NULL);

    DealStr dstr;
    //查询成功
    if(nRow > 0)
    {
        // cout << "Find User In Database" << endl;
        //将商品信息写入s_send
        for(int i=1; i<=nRow; i++) //从1开始，0行是属性名字
        {
            s_info.com_array[i-1].identifier = dstr.str_To_Num(pResult[nCol*i+1]); //唯一标识
            {
                // cout << "length:" << strlen(pResult[nCol*i+2]) << endl;
                // char ss[20];
                // memcpy(ss,pResult[nCol*i+2],strlen(pResult[nCol*i+2]));
                // cout << ss << endl;
                // exit(-1);
            }
            //这里+1防止乱码
            memcpy(s_info.com_array[i-1].cname,pResult[nCol*i+2],strlen(pResult[nCol*i+2])+1); //商品名
            memcpy(s_info.com_array[i-1].brand,pResult[nCol*i+3],strlen(pResult[nCol*i+3])+1); //商品品牌
            memcpy(s_info.com_array[i-1].classify,pResult[nCol*i+4],strlen(pResult[nCol*i+4])+1); //商品类别

            s_info.com_array[i-1].price = dstr.str_To_Num(pResult[nCol*i+5]); //价格
            s_info.com_array[i-1].remain = dstr.str_To_Num(pResult[nCol*i+6]); //剩余数目
            s_info.com_array[i-1].frozen = dstr.str_To_Num(pResult[nCol*i+7]); //冻结数目
            memcpy(s_info.com_array[i-1].others,pResult[nCol*i+8],strlen(pResult[nCol*i+8])+1); //商品额外信息
        }
       
        {
            //打印
            // cout << "数据库读出的信息" << endl;
            // for(int i=0; i<=nRow; i++)
            // {
            //     for(int j=0; j<nCol; j++)
            //     {
            //         cout << pResult[nCol*i+j] << "\t";
            //     }
            //     cout << endl;
            // }

            // cout << "写入信息重读" << endl;
            // for(int i=0; i<nRow; i++)
            // {
            //     cout << s_info.com_array[i].identifier << "\t"
            //         << s_info.com_array[i].cname << "\t"
            //         << s_info.com_array[i].brand << "\t"
            //         << s_info.com_array[i].classify << "\t"
            //         << s_info.com_array[i].price << "\t"
            //         << s_info.com_array[i].remain << "\t"
            //         << s_info.com_array[i].frozen << "\t"
            //         << s_info.com_array[i].others << endl;
            // }
        }
        
        return nRow;
    }
    //查询失败
    else
    {
        return 0;
    }
}

int Database::get_All_Commodity_Info(info& s_info,char* sql)
{
    //char* sql = "select * from goods;";
    rc = sqlite3_get_table(db, sql, &pResult, &nRow, &nCol, NULL);

    DealStr dstr;
    //查询成功
    if(nRow > 0)
    {
        // cout << "Find User In Database" << endl;
        //将商品信息写入s_send
        for(int i=1; i<=nRow; i++) //从1开始，0行是属性名字
        {
            s_info.com_array[i-1].identifier = dstr.str_To_Num(pResult[nCol*i+1]); //唯一标识
            {
                // cout << "length:" << strlen(pResult[nCol*i+2]) << endl;
                // char ss[20];
                // memcpy(ss,pResult[nCol*i+2],strlen(pResult[nCol*i+2]));
                // cout << ss << endl;
                // exit(-1);
            }
            //这里+1防止乱码
            memcpy(s_info.com_array[i-1].cname,pResult[nCol*i+2],strlen(pResult[nCol*i+2])+1); //商品名
            memcpy(s_info.com_array[i-1].brand,pResult[nCol*i+3],strlen(pResult[nCol*i+3])+1); //商品品牌
            memcpy(s_info.com_array[i-1].classify,pResult[nCol*i+4],strlen(pResult[nCol*i+4])+1); //商品类别

            s_info.com_array[i-1].price = dstr.str_To_Num(pResult[nCol*i+5]); //价格
            s_info.com_array[i-1].remain = dstr.str_To_Num(pResult[nCol*i+6]); //剩余数目
            s_info.com_array[i-1].frozen = dstr.str_To_Num(pResult[nCol*i+7]); //冻结数目
            memcpy(s_info.com_array[i-1].others,pResult[nCol*i+8],strlen(pResult[nCol*i+8])+1); //商品额外信息
        }
       
        {
            //打印
            // cout << "数据库读出的信息" << endl;
            // for(int i=0; i<=nRow; i++)
            // {
            //     for(int j=0; j<nCol; j++)
            //     {
            //         cout << pResult[nCol*i+j] << "\t";
            //     }
            //     cout << endl;
            // }

            // cout << "写入信息重读" << endl;
            // for(int i=0; i<nRow; i++)
            // {
            //     cout << s_info.com_array[i].identifier << "\t"
            //         << s_info.com_array[i].cname << "\t"
            //         << s_info.com_array[i].brand << "\t"
            //         << s_info.com_array[i].classify << "\t"
            //         << s_info.com_array[i].price << "\t"
            //         << s_info.com_array[i].remain << "\t"
            //         << s_info.com_array[i].frozen << "\t"
            //         << s_info.com_array[i].others << endl;
            // }
        }
        
        return nRow;
    }
    //查询失败
    else
    {
        return 0;
    }
}

double Database::get_User_Balance(const string& name)
{
    //构造sql语句
    string sql = "select * from user where username = '";
    sql = sql + name + "';";
    // cout << "查询商家余额语句：" << sql << endl;
    //查库
    rc = sqlite3_get_table(db, (char*)sql.data(), &pResult, &nRow, &nCol, NULL);
    if (rc == SQLITE_OK)
    {
       return (double)DealStr().str_To_Num(pResult[nCol+4]);
    }
    else
    {
        cout << "数据库不存在该表" << endl;
        return -1.0;
    }
}

bool Database::get_Com_Info(int iden,commodity& com)
{
    string sql = "select * from goods where identifier = " + to_string(iden);
    cout << "查询商品信息:" << sql << endl;
    rc = sqlite3_get_table(db, (char*)sql.data(), &pResult, &nRow, &nCol, NULL);
    if(nRow > 0)
    {
        DealStr dl;
        com.identifier = dl.str_To_Num(pResult[nCol+1]);
        strcpy(com.cname,pResult[nCol+2]);
        strcpy(com.brand,pResult[nCol+3]);
        strcpy(com.classify,pResult[nCol+4]);
        com.price = (double)dl.str_To_Num(pResult[nCol+5]);
        com.remain = dl.str_To_Num(pResult[nCol+6]);
        com.frozen = dl.str_To_Num(pResult[nCol+7]);
        strcpy(com.others,pResult[nCol+8]);
        return true;
    }
    else
    {
        return false;
    }

}
